<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include/src :: form_base_js_and_css"/>
</head>
<body style="background-color: #f2f2f2; height: 100%;">
	
	<script type="text/html" id="toolbar_in_form">
	</script>

	<div id="id_panel">
	</div>
	<script th:inline="javascript">
	
	var dto = [[${dto}]]
	dto.saveBtn.pathCommit = '/api/inter/tccAssemble/doError';
	console.log(dto);
	
	// 表单初始化
	var formObj = null;
	//使用layui的sku模块
	layui.config({
		//自定义layui组件的目录,设定扩展的layui模块的所在目录，一般用于外部模块扩展
		base: '/static/js/layui_extends/'
	}).use(['form', 'jquery', 'laydate', 'YsForm', 'YsModal'], function(){
		var $ = layui.jquery,
		form = layui.form,
		laydate = layui.laydate,
		YsForm = layui.YsForm,
		YsModal = layui.YsModal
		/* 
		* 表单初始化
		* 参数1：表单配置参数
		* 参数2：子表配置参数
		*/
		formObj = YsForm.init(
		{
			id:'id_panel', // 页面容器ID
			dto: $.parseJSON(JSON.stringify(dto)),
			callback: function() {
				
			}
		}
		);
		
		/*
		 * 监听币种输入框
		 */
        var domForm = $('#' + window.formObj.c.id_form);
        domForm.find('input[name="exchangeRate"]').on('propertychange',function (e) {
        	calFormItem();
		});
        domForm.find('input[name="exchangeRate"]').on('change',function (e) {
        	calFormItem();
		});
	});
	</script>
	
	<script>

	
	/*
	 * 自动计算表单数据
	 */
	function calFormItem() {
		
	    var domForm = $('#' + window.formObj.c.id_form);
		var exchangeRate = parseFloat(domForm.find('input[name="exchangeRate"]').val()); // 计算汇率
		
		var sonlist = formObj.c.subGridObj.c.sonlist;
		for (var i = 0; i < sonlist.length; i++) {
			formObj.c.subGridObj.c.sonlist[i]['amountCny'] = exchangeRate ? Math.round((sonlist[i].amount) * exchangeRate * 100) / 100 : 0
		}
		formObj.c.subGridObj.reloadGrid();
		
		// 计算付款外币总金额、付款CNY总金额
		if(exchangeRate) {
			var amountTotalLocal = 0;
			var amountTotalCny = 0;
			for (var i = 0; i < sonlist.length; i++) {
				var amount = parseFloat(sonlist[i]['amount']);
				if(amount) {
					amountTotalLocal = amountTotalLocal + amount;
					amountTotalCny = amountTotalCny + amount * exchangeRate;
				}
			}
			domForm.find('input[name="amountTotalLocal"]').val(amountTotalLocal);
			domForm.find('input[name="amountTotalCny"]').val(Math.round(amountTotalCny * 100) / 100.00);
		}
		
	}
	</script>

</body>
</html>
